package fylder.recycler.demo;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import butterknife.Bind;
import butterknife.ButterKnife;
import fylder.recycler.demo.adapter.TestAdapter;
import fylder.recycler.demo.view.EndlessRecyclerOnScrollListener;
import fylder.recycler.demo.view.FylderRecyclerView;

/**
 * 演示
 */
public class DemoActivity extends AppCompatActivity {

    @Bind(R.id.demo_refresh)
    SwipeRefreshLayout refreshLay;
    @Bind(R.id.demo_recycler)
    FylderRecyclerView recyclerView;

    Handler handler = new Handler(Looper.getMainLooper());
    LinearLayoutManager manager;
    TestAdapter adpater;

    int oneSize = 6;

    int size = 0;

    boolean isEnd = false;
    boolean isDown = false;
    boolean isLoading = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        ButterKnife.bind(this);
        manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        refreshLay.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isLoading) {
                    isLoading = true;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            isLoading = false;
                            isEnd = false;
                            refreshLay.setRefreshing(false);
                            size = oneSize;
                            adpater.refresh(size);
                        }
                    }, 1000);
                } else {
                    refreshLay.setRefreshing(false);
                }
            }
        });
        recyclerView.setLoadingListener(new FylderRecyclerView.LoadingListener() {
            @Override
            public void loading() {
                if (!isLoading) {
                    isLoading = true;

                    if (isEnd) {
                        Log.w("123", "load end");
                        isLoading = false;
                    } else {
                        adpater.loading();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                isLoading = false;
                                isDown = false;
                                size += oneSize;
                                if (size > oneSize * 3) {
                                    isEnd = true;
                                    adpater.ending(size);
                                } else {
                                    isEnd = false;
                                    adpater.refresh(size);
                                }
                            }
                        }, 2000);
                    }
                }
            }
        });
        adpater = new TestAdapter(this);
        recyclerView.setAdapter(adpater);
    }
}
